<template>
  <div>
    <div id="myChart" :style="{width:'870px',height:'392px'}"></div>
  </div>
</template>

<script>
let echarts = require("echarts/lib/echarts");
require("echarts/lib/chart/pie");
require("echarts/lib/component/tooltip");
require("echarts/lib/component/toolbox");
require("echarts/lib/component/title");

export default {
  data() {
    return {
    };
  },
  props: {
    titleText:{
      type:String
    },
    yAxisData:{
      type:Array
    }
  },
  watch: {
    yAxisData(newValue, oldValue) {
      this.yAxisData = newValue;
      this.drawPie();
    },
    titleText(newValue, oldValue){
      this.titleText = newValue;
      this.drawPie();
    }
  },
  mounted() {
    this.drawPie();
  },
  methods: {
    drawPie() {
      let chartsContainer = document.getElementById("myChart");
      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(chartsContainer);
      let self = this;
      let option = {
        title: {
          text: self.titleText,
          left: "center",
          textStyle: {
            color: "#333",
            fontSize: 16,
            fontWeight: "normal"
          }
        },
        tooltip: {
          trigger: "item"
        },
        series: [
          {
            type: "pie",
            radius: "65%",
            center: ["50%", "50%"],
            selectedMode: "single",
            data: this.yAxisData,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)"
              }
            },
            label:{
              formatter: '{b},{c}'
            },
            itemStyle:{
              color: function(params) {
                  // build a color map as your need.
                  var colorList = [
                    "#E03234",
                    "#8424A0",
                    "#3443A0",
                    "#1793DF",
                    "#17A4B9",
                    "#3F9743",
                    "#B9C339",
                    "#FDAA24",
                    "#F14826",
                    "#4C6570"
                  ];
                  return colorList[params.dataIndex];
                }
            }
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>